#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
个人中心密码修改功能测试运行脚本
"""

import os
import sys
import json
import time
import logging
import pytest
from datetime import datetime

# 添加项目根目录到系统路径
project_root = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, project_root)

# 导入测试模块
from tests.utilities.logger import setup_logger

def create_password_test_data():
    """创建密码修改测试数据文件"""
    password_data = {
        "login_credentials": {
            "username": "admin",
            "password": "123456"
        },
        "test_cases": [
            {
                "id": "TC1",
                "old_password": "123456",
                "new_password": "666666",
                "confirm_password": "666666",
                "expected_result": "修改密码成功"
            },
            {
                "id": "TC2",
                "old_password": "666666",
                "new_password": "123456",
                "confirm_password": "123456",
                "expected_result": "修改密码成功"
            },
            {
                "id": "TC3",
                "old_password": "123456",
                "new_password": "",
                "confirm_password": "",
                "expected_result": "密码不能为空"
            },
            {
                "id": "TC4",
                "old_password": "",
                "new_password": "888888",
                "confirm_password": "888888",
                "expected_result": "原密码不能为空"
            },
            {
                "id": "TC5",
                "old_password": "123456",
                "new_password": "888888",
                "confirm_password": "999999",
                "expected_result": "两次输入密码不一致"
            },
            {
                "id": "TC6",
                "old_password": "123456",
                "new_password": "12345",
                "confirm_password": "12345",
                "expected_result": "密码长度不能小于6位"
            },
            {
                "id": "TC7",
                "old_password": "123456",
                "new_password": "1234567890123456789012345",
                "confirm_password": "1234567890123456789012345",
                "expected_result": "密码长度不能大于20位"
            },
            {
                "id": "TC8",
                "old_password": "123456",
                "new_password": "123456",
                "confirm_password": "123456",
                "expected_result": "新密码不能与原密码相同"
            },
            {
                "id": "TC9",
                "old_password": "wrongpassword",
                "new_password": "888888",
                "confirm_password": "888888",
                "expected_result": "原密码错误"
            },
            {
                "id": "TC10",
                "old_password": "123456",
                "new_password": "abc123",
                "confirm_password": "abc123",
                "expected_result": "修改密码成功"
            }
        ]
    }
    
    # 确保测试数据目录存在
    test_data_dir = os.path.join(project_root, "tests", "test_data")
    os.makedirs(test_data_dir, exist_ok=True)
    
    # 保存测试数据到文件
    password_data_file = os.path.join(test_data_dir, "password_data.json")
    with open(password_data_file, 'w', encoding='utf-8') as f:
        json.dump(password_data, f, ensure_ascii=False, indent=4)
    
    print(f"密码修改测试数据已创建: {password_data_file}")
    return password_data_file

def run_password_change_test():
    """运行密码修改测试"""
    # 创建测试数据
    create_password_test_data()
    
    # 设置日志
    logger = setup_logger()
    logging.info("开始运行密码修改测试")
    
    # 创建报告目录
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    report_dir = os.path.join(project_root, "tests", "reports")
    os.makedirs(report_dir, exist_ok=True)
    report_file = os.path.join(report_dir, f"password_change_test_report_{timestamp}.html")
    
    try:
        # 使用pytest运行测试
        logging.info("使用pytest运行密码修改测试")
        
        # 设置pytest命令行参数
        pytest_args = [
            "-v",                                # 详细输出
            "--html=" + report_file,             # HTML报告
            "--self-contained-html",             # 自包含HTML报告
            "-k", "test_03_change_password",     # 只运行密码修改测试
            os.path.join(project_root, "tests", "test_cases", "test_profile.py")  # 测试文件路径
        ]
        
        # 运行pytest
        result = pytest.main(pytest_args)
        
        # 输出测试结果
        if result == 0:
            logging.info("密码修改测试执行成功")
            print(f"测试成功! 报告已保存到: {report_file}")
        else:
            logging.error("密码修改测试执行失败")
            print(f"测试失败! 报告已保存到: {report_file}")
        
        # 测试完成
        logging.info("密码修改测试执行完成")
        
    except Exception as e:
        logging.error(f"测试执行过程中出错: {e}")
        import traceback
        logging.error(traceback.format_exc())
        
    logging.info("测试资源已清理")

if __name__ == "__main__":
    run_password_change_test() 